// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   RMatrix.java

package com.java4less.rchart;


class RMatrix
{

	private int rows;
	private int columns;
	private double data[][];

	public RMatrix(double d[][])
	{
		rows = d.length;
		columns = d[0].length;
		data = d;
	}

	public RMatrix(int d[][])
	{
		rows = d.length;
		columns = d[0].length;
		data = new double[rows][columns];
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				data[i][j] = d[i][j];

		}

	}

	public RMatrix(int prows, int pcolumns)
	{
		rows = prows;
		columns = pcolumns;
		data = new double[rows][columns];
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				data[i][j] = 0.0D;

		}

	}

	public RMatrix add(RMatrix m2)
	{
		RMatrix newMatrix = new RMatrix(rows, columns);
		if (rows == m2.rows && columns == m2.columns)
		{
			for (int i = 0; i < rows; i++)
			{
				for (int j = 0; j < columns; j++)
					newMatrix.data[i][j] = data[i][j] + m2.data[i][j];

			}

		}
		return newMatrix;
	}

	public RMatrix invert()
	{
		int n = data.length;
		double D[][] = new double[n + 1][2 * n + 2];
		int i;
		for (i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				D[i + 1][j + 1] = data[i][j];

		}

		int error = 0;
		int n2 = 2 * n;
		for (i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				D[i][j + n] = 0.0D;

			D[i][i + n] = 1.0D;
		}

		i = 1;
		do
		{
			if (i > n)
				break;
			double alpha = D[i][i];
			if (alpha == 0.0D)
			{
				error = 1;
				break;
			}
			for (int j = 1; j <= n2; j++)
				D[i][j] = D[i][j] / alpha;

			for (int k = 1; k <= n; k++)
			{
				if (k - i == 0)
					continue;
				double beta = D[k][i];
				for (int j = 1; j <= n2; j++)
					D[k][j] = D[k][j] - beta * D[i][j];

			}

			i++;
		} while (true);
		RMatrix m = new RMatrix(rows, columns);
		for (i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				m.data[i][j] = D[i + 1][j + 1 + n];

		}

		return m;
	}

	public RMatrix mult(double v)
	{
		RMatrix newMatrix = new RMatrix(rows, columns);
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				newMatrix.data[i][j] = v * data[i][j];

		}

		return newMatrix;
	}

	public void setValue(int r, int c, double v)
	{
		data[r][c] = v;
	}

	public double getValue(int r, int c)
	{
		return data[r][c];
	}

	public RMatrix mult(RMatrix m2)
	{
		RMatrix m1 = this;
		RMatrix newMatrix = new RMatrix(m1.rows, m2.columns);
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < m2.columns; j++)
			{
				for (int k = 0; k < columns; k++)
					newMatrix.data[i][j] += data[i][k] * m2.data[k][j];

			}

		}

		return newMatrix;
	}

	public RMatrix transpose()
	{
		RMatrix newMatrix = new RMatrix(columns, rows);
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
				newMatrix.data[j][i] = data[i][j];

		}

		return newMatrix;
	}
}
